var main = plus.android.runtimeMainActivity();
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
var BAdapter = BluetoothAdapter.getDefaultAdapter();
var receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver')
let bluetool = {

	//发现设备
	discovery: function(found, DISCOVERY_FINISHED) {
		return new Promise((resolve, reject) => {

			console.log("开始搜索设备");
			var filter = new IntentFilter();
			var bdevice = new BluetoothDevice();
			BAdapter.startDiscovery(); //开启搜索  
			var receiver;
			var un = null;
			receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
				onReceive: function(context, intent) { //实现onReceiver回调函数  
					plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作  
					console.log(intent.getAction()); //获取action  
					if (intent.getAction() ==
						"android.bluetooth.adapter.action.DISCOVERY_FINISHED") {
						main.unregisterReceiver(receiver); //取消监听  
						DISCOVERY_FINISHED()
					} else {
						var BleDevice = intent.getParcelableExtra(BluetoothDevice
							.EXTRA_DEVICE);

						found({
							name: BleDevice.getName(),
							deviceId: BleDevice.getAddress(),
							intent: intent
						})
						//判断是否配对  
						if (BleDevice.getBondState() == bdevice.BOND_NONE) {
							console.log("未配对蓝牙设备：" + BleDevice.getName() + '    ' +
								BleDevice.getAddress());
						} else {
							if (BleDevice.getName() != un) { //判断防止重复添加  
								console.log("已配对蓝牙设备：" + BleDevice.getName() + '    ' +
									BleDevice.getAddress());
								un = BleDevice.getName();

							}
						}
					}

				}
			});

			filter.addAction(bdevice.ACTION_FOUND);
			filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
			filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
			filter.addAction(BAdapter.ACTION_STATE_CHANGED);

			main.registerReceiver(receiver, filter); //注册监听  
		})
	},
	//停止搜索
	unregisterReceiver: function() {
		main.unregisterReceiver(receiver);

	},
	//创建连接
	createConnection: function(item) {

		this.unregisterReceiver()

		return new Promise((resolve, reject) => {

			var intent = item.intent
			plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作  
			var BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
			//判断是否配对  

			let mac_address = item.deviceId

			if (BleDevice.getBondState() == bdevice.BOND_NONE) {
				if (BleDevice.createBond()) {
					console.log("配对成功")
					this.connect(mac_address, resolve, reject)

				}
			} else {
				console.log("已经配对")
				this.connect(mac_address, resolve, reject)
			}

		})
	},

	connect: function(mac_address, resolve, reject) {
		var main = plus.android.runtimeMainActivity();
		var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
		var UUID = plus.android.importClass("java.util.UUID");
		var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
		var BAdapter = BluetoothAdapter.getDefaultAdapter();
		var device = BAdapter.getRemoteDevice(mac_address);
		plus.android.importClass(device);
		var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
		plus.android.importClass(bluetoothSocket);

		if (!bluetoothSocket.isConnected()) {
			console.log('检测到设备未连接，尝试连接....');
			bluetoothSocket.connect();

		}
		if (!bluetoothSocket.isConnected()) {
			reject()
		} else {
			resolve()
		}
	}


}
export default bluetool
